/**
 * jQuery 版quickbar
 * @author yulipu
 * 注意: require History.js
 */
function QuickbarJQ() {
	this.sliderContentId = 'ySliderContent';  // 滑动区域 显示内容域
	this.sliderArea = 'ySlider';  // 滑动容器
	this.quickbarArea = 'yQuickbar';  // quickbar
	this.openQuickbarArea = 'yQuickbarOpen';

	this.sliderCloseBtnId = 'ySliderCloseBtn';  // slider 关闭按钮 id
	this.viewHistoryBtnId = 'yQuickbarMyViewBtn';  // 浏览记录按钮 id
	this.mySelfBtnId = 'yQuickbarMySelfBtn';  // 我自己按钮 id
	this.quickbarCloseBtnId = 'yQuickbarCloseBtn';  // quickbar 关闭按钮 id
	this.quickbarOpenBtnId = 'yQuickbarOpenBtn';  // quickbar 打开按钮 id
	this.domSize = null;
	
	this.history = new History('y_art'); // history 对象
	this.noHistoryHtml = '<div class="y-noHistory" title="什么都没有哦~"></div>';
	this.haveHistoryHtml = 
'<a class="y-history-item" href="{$link}" target="_blank"><div class="y-history-item-sawtooth"></div><div class="y-history-item-padding"><p >{$title}</p></div></a>';
	this.clearHistoryHtml = '<div class="y-clearHistory"><a href="javascript:;" id="yClearHistory">删除历史记录</a></div>';
}
QuickbarJQ.prototype = {
	constructor : QuickbarJQ
	,openCloseSlider : function(f) {
		var _self = this;
		if(0 == f) {
			jQuery('#'+_self.sliderArea).animate({left: '-235px'}, "quick");  // close
		} else {
			jQuery('#'+_self.sliderArea).animate({left: '36px'}, "quick");  // open  边栏占 36px
		}
	}
	,openCloseQuickbar : function(f) {
		var _self = this;
		if(0 == f) {
			// 先关闭滑动内容区
			_self.openCloseSlider(0);
			jQuery('#'+_self.quickbarArea).animate({left: '-36px'}, "quick");  // close  边栏占 36px
			jQuery('#'+_self.openQuickbarArea).animate({left: '38px'}, "quick");  // 打开按钮显示
		} else {
			jQuery('#'+_self.quickbarArea).animate({left: '0'}, "quick");  // open 
			jQuery('#'+_self.openQuickbarArea).animate({left: '-60px'}, "quick");  // 隐藏按钮显示
		}
	}
	,initClose : function() {
		var _self = this;
		// 滑动内容区关闭按钮
		jQuery('#'+_self.sliderCloseBtnId).click(function(){
			_self.openCloseSlider(0);
		});
		// 浏览记录按钮 打开滑动内容区
		jQuery('#'+_self.viewHistoryBtnId).click(function(){
			_self.openCloseSlider(1);
		});
		// 侧边关闭按钮
		jQuery('#'+_self.quickbarCloseBtnId).click(function(){
			_self.openCloseQuickbar(0);
		});
		// 侧边打开按钮
		jQuery('#'+_self.quickbarOpenBtnId).click(function(){
			_self.openCloseQuickbar(1);
		});
	}
	,resizeContentHeight : function() {
		this.domSize = {
			"width" : Math.max(document.documentElement.clientWidth, document.body.clientWidth),
			"height" : Math.max(document.documentElement.clientHeight, 0/*document.body.clientHeight*/)
		};
		document.getElementById(this.sliderContentId).style.height = this.domSize.height - 160 + 'px';  // 多减20px. 关闭按钮区 40px, userinfo区 100px
	}
	,initSliderContent : function() {
		var _self = this;
		jQuery(window).resize(function(){
			_self.resizeContentHeight();
		});
		
		_self.resizeContentHeight();
	}
	,renderHistoryItem : function(jsonItem) {
		var s = this.haveHistoryHtml.replace('{$link}', jsonItem.link);
		s = s.replace('{$title}', jsonItem.title);
		
		return s;
	}
	,clearHistory : function() {
		this.history.clearHistory();
		this.initHistory();  // 重新加载数据
	}
	,initHistory : function() {
		var _self = this;
		var s = '';
		var json = this.history.getList();  // Array or null
		
		if(null != json) {
			var len = json.length;
			for(var i=0; i<len; i++) {
				s += this.renderHistoryItem(json[i]);
			}
			s += this.clearHistoryHtml;  // 添加清除按钮
			
		} else {
			s = this.noHistoryHtml;
		}
		jQuery('#'+this.sliderContentId).html(s);
		
		if(document.getElementById('yClearHistory')) {
			document.getElementById('yClearHistory').onclick = function(){
				_self.clearHistory();
			};
		}
	}
	,initMySelf : function() {
		jQuery('#'+this.mySelfBtnId).click(function(){
			alert('mySelf');
		});
	}
	,init : function() {
		this.initClose();  //关闭处理
		this.initSliderContent();  // 初始化滑动区大小
		
		this.initHistory();  // 浏览记录显示
		
		this.initMySelf();  // 我自己
		
		return this;
	}
};
